package com.example.app01.model

import java.util.Random

class Deck() {
    private val cards = mutableListOf<Card>()
    private val r = Random()

    init {
        reset()
    }

    private fun reset() {
        cards.clear()
        // 遍历所有花色和数字组合生成52张标准扑克牌
        for (suit in Card.validSuits) { // ["♥", "♦", "♠", "♣"]
            for (rank in Card.rankStrings) { // ["A", "2", ..., "K"]
                val card = Card(suit = suit, rank = rank)
                cards.add(card)
            }
        }
    }

    fun drawRandomCard(): Card? {
        if (cards.isEmpty()) {
            reset()
        }

        return if (cards.isNotEmpty()) {
            cards.removeAt(r.nextInt(cards.size))
        } else {
            null
        }
    }

    fun getRemainingCount(): Int {
        return cards.size
    }
}